package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcCaigouDetailMapper;
import com.lxmes.business.domain.CcCaigouDetail;
import com.lxmes.business.service.ICcCaigouDetailService;

/**
 * 采购详情Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-06-11
 */
@Service
public class CcCaigouDetailServiceImpl implements ICcCaigouDetailService 
{
    @Autowired
    private CcCaigouDetailMapper ccCaigouDetailMapper;
    @Autowired
    private JcIteminfoMapper jcIteminfoMapper;

    /**
     * 查询采购详情
     * 
     * @param id 采购详情主键
     * @return 采购详情
     */
    @Override
    public CcCaigouDetail selectCcCaigouDetailById(Long id)
    {
        return ccCaigouDetailMapper.selectCcCaigouDetailById(id);
    }

    /**
     * 查询采购详情
     *
     * @param danhao 采购单号
     * @return 采购详情
     */
    @Override
    public List<CcCaigouDetail> selectCcCaigouDetailByDanHao(String danhao) {
        return ccCaigouDetailMapper.selectCcCaigouDetailByDanHao(danhao);
    }

    /**
     * 查询采购详情列表
     * 
     * @param ccCaigouDetail 采购详情
     * @return 采购详情
     */
    @Override
    public List<CcCaigouDetail> selectCcCaigouDetailList(CcCaigouDetail ccCaigouDetail)
    {
        return ccCaigouDetailMapper.selectCcCaigouDetailList(ccCaigouDetail);
    }

    @Override
    public List<CcCaigouDetail> addCaigoItemInfo(Long[] ids) {
        List<CcCaigouDetail>  caigouDetailList =new ArrayList<>();
        List<JcIteminfo> jcIteminfoList = jcIteminfoMapper.selecIteminfoByIds(ids);
        if(jcIteminfoList.size()>0){
            for (JcIteminfo info :jcIteminfoList) {
                CcCaigouDetail caigouDetail=new CcCaigouDetail();
                caigouDetail.setItemCode(info.getItemCode());
                caigouDetail.setItemName(info.getItemName());
                caigouDetail.setClassId(info.getClassId());
                caigouDetail.setClassName(info.getClassName());
                caigouDetail.setSpec(info.getSpec());
                caigouDetail.setSupplyId(info.getSupplyId());
                caigouDetail.setSupplyName(info.getSupplyName());
                caigouDetail.setUnit(info.getUnit());
                caigouDetail.setUnitPrice(new BigDecimal("0.00"));
                caigouDetail.setTotalPrice(new BigDecimal("0.00"));
                caigouDetail.setCaigouNum(new BigDecimal("0") );
              /*  if(info.getPrice()!=null){
                    caigouDetail.setCankaoPrice(info.getPrice());
                }else {
                    caigouDetail.setCankaoPrice(new BigDecimal("0.00"));
                }
                Map<String, String> map=new HashMap<>();
                map = ccCaigouDetailMapper.getLastCaiGouItem(info.getItemCode());
                if(StringUtils.isNotEmpty(map)){
                    caigouDetail.setLastPrice(new BigDecimal(String.valueOf(map.get("lastPrice"))));
                    caigouDetail.setLastDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,String.valueOf(map.get("caigouDate"))));
                }else {
                    caigouDetail.setLastPrice(new BigDecimal("0.00"));
                }*/
                caigouDetailList.add(caigouDetail);
            }
        }
        return caigouDetailList;
    }

    /**
     * 新增采购详情
     * 
     * @param ccCaigouDetail 采购详情
     * @return 结果
     */
    @Override
    public int insertCcCaigouDetail(CcCaigouDetail ccCaigouDetail)
    {
        ccCaigouDetail.setCreateTime(DateUtils.getNowDate());
        return ccCaigouDetailMapper.insertCcCaigouDetail(ccCaigouDetail);
    }

    /**
     * 修改采购详情
     * 
     * @param ccCaigouDetail 采购详情
     * @return 结果
     */
    @Override
    public int updateCcCaigouDetail(CcCaigouDetail ccCaigouDetail)
    {
        ccCaigouDetail.setUpdateTime(DateUtils.getNowDate());
        return ccCaigouDetailMapper.updateCcCaigouDetail(ccCaigouDetail);
    }

    /**
     * 批量删除采购详情
     * 
     * @param ids 需要删除的采购详情主键
     * @return 结果
     */
    @Override
    public int deleteCcCaigouDetailByIds(Long[] ids)
    {
        return ccCaigouDetailMapper.deleteCcCaigouDetailByIds(ids);
    }

    /**
     * 删除采购详情信息
     * 
     * @param id 采购详情主键
     * @return 结果
     */
    @Override
    public int deleteCcCaigouDetailById(Long id)
    {
        return ccCaigouDetailMapper.deleteCcCaigouDetailById(id);
    }
}
